package hutech.ksv.dao;

import hutech.ksv.entity.QuanTriVienEntity;
import hutech.ksv.entity.SinhVienEntity;
import hutech.ksv.entity.TaiKhoanQTVEntity;
import hutech.ksv.entity.ThongTinTaiKhoanAdmin;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class AdminAccountDao implements Serializable {
	
	@Autowired
	DataSource dataSource;

	public List<ThongTinTaiKhoanAdmin> getThongTinTaiKhoanAdmin() {
		String query = "SELECT * FROM `taikhoanqtv` "
				+ "INNER JOIN `chitietquyen` ON (`taikhoanqtv`.`TenTKQTV` = `chitietquyen`.`TenTKQTV`) "
				+ "INNER JOIN `quantrivien` ON (`taikhoanqtv`.`MaQTV` = `quantrivien`.`MaQTV`)";
		List<ThongTinTaiKhoanAdmin> listThongTinTaiKhoanAdmin = new ArrayList<ThongTinTaiKhoanAdmin>();

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				ThongTinTaiKhoanAdmin thongTinTaiKhoanAdmin = new ThongTinTaiKhoanAdmin();
				thongTinTaiKhoanAdmin.setTenTKQTV(rs.getString("TenTKQTV"));
				thongTinTaiKhoanAdmin.setMatKauQTV(rs.getString("MatKhauQTV"));
				thongTinTaiKhoanAdmin.setMaQTV(rs.getInt("MaQTV"));
				thongTinTaiKhoanAdmin.setTenQTV(rs.getString("TenQTV"));
				thongTinTaiKhoanAdmin.setDiaChiQTV(rs.getString("DiaChiQTV"));
				thongTinTaiKhoanAdmin.setGioiTinhQTV(rs.getString("GioiTinhQTV"));
				thongTinTaiKhoanAdmin.setSdtQTV(rs.getString("SDTQTV"));
				thongTinTaiKhoanAdmin.setEmailQTV(rs.getString("EmailQTV"));
				listThongTinTaiKhoanAdmin.add(thongTinTaiKhoanAdmin);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listThongTinTaiKhoanAdmin;
	}
	
	public ArrayList<String> getTatCaTaiKhoanAdmin() {
		String query = "SELECT * FROM `taikhoanqtv`";

		ArrayList<String> danhSachTenTaiKhoan = new ArrayList<String>();
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			ResultSet rs = preparedStatement.executeQuery();
			String tenTaiKhoan = "";
			while (rs.next()) {
				tenTaiKhoan = rs.getString("TenTKQTV");
				danhSachTenTaiKhoan.add(tenTaiKhoan);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return danhSachTenTaiKhoan;
	}
	
	public int themQuanTriVien(QuanTriVienEntity quanTriVienEntity) {
		String query = "INSERT INTO `quantrivien` (`TenQTV`,`DiaChiQTV`,`GioiTinhQTV`,`SDTQTV`,`EmailQTV`) "
				+ "VALUES (?,?,?,?,?)";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query,
					Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setString(1, quanTriVienEntity.getTenQTV());
			preparedStatement.setString(2, quanTriVienEntity.getDiaChiQTV());
			preparedStatement.setString(3, quanTriVienEntity.getGioiTinhQTV());
			preparedStatement.setString(4, quanTriVienEntity.getSdtQTV());
			preparedStatement.setString(5, quanTriVienEntity.getEmailQTV());
			preparedStatement.execute();
			ResultSet rs = preparedStatement.getGeneratedKeys();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
	
	public int xoaQuanTriVienVuaNhap(int maQTV) {
		String query = "DELETE FROM `quantrivien` WHERE `MaQTV` = ?";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query,
					Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setInt(1, maQTV);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
	
	public int themTaiKhoanQuanTriVien(TaiKhoanQTVEntity taiKhoanQTVEntity) {
		String query = "INSERT INTO `taikhoanqtv` (`TenTKQTV`,`MatKhauQTV`,`MaQTV`) "
				+ "VALUES (?,?,?)";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query,
					Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setString(1, taiKhoanQTVEntity.getTenTKQTV());
			preparedStatement.setString(2, taiKhoanQTVEntity.getMatKhauQTV());
			preparedStatement.setInt(3, taiKhoanQTVEntity.getMaQTV());
			preparedStatement.execute();
			ResultSet rs = preparedStatement.getGeneratedKeys();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

}
